package com.easyshopping;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.easyshopping.domain.ItemCategory;
import com.easyshopping.facade.ShoppingListFacade;

public class AddItemActivity extends Activity implements OnClickListener {

	// private String[] categories = { "Bread", "Cheese", "Others" };
	private Cursor mCursor_itemCategorylist;

	private ListView ls_categories;
	private Button btn_addItem;
	private Button btn_Back;
	private Button btn_addCategory;
	private EditText et_item;

//	private int itemPos = -2;
	
	private int selected = -1;
	
	public void updateCategories() {
		// cateAdapter.notifyDataSetChanged();
	}

	//
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.add_item);

		ls_categories = (ListView) this.findViewById(R.id.Categories);
		fillItemCategoryList();

		btn_addItem = (Button) this.findViewById(R.id.Btn_Add_Item);
		btn_addItem.setOnClickListener(this);

		btn_Back = (Button) this.findViewById(R.id.Btn_Back2shoppinglist);
		btn_Back.setOnClickListener(this);
		
		btn_addCategory = (Button) this
				.findViewById(R.id.Btn_Open_AddCategoryPage);
		btn_addCategory.setOnClickListener(this);

		et_item = (EditText) this.findViewById(R.id.ItemText);
	}

	public void onClick(View view) {

		if(view.equals(btn_Back)){
			this.finish();
			return;
		}
		
		if (view.equals(btn_addItem)) {
			if (et_item.getText().toString().trim().length() != 0) {
				
				String itemText = et_item.getText().toString();
				
				int cat_id = 1;
				if (this.getSelected() != -1) {
					mCursor_itemCategorylist.moveToPosition(this.getSelected());
					cat_id  = mCursor_itemCategorylist.getInt(
							mCursor_itemCategorylist.getColumnIndex(ItemCategory.ITEMCATEGORY_ID));
				}
				ShoppingListFacade.getInstance().addItem(itemText, cat_id);
				et_item.setText("");
				et_item.setHint(R.string.continue2add);
			}
			return;
		} 
		
		if (view.equals(btn_addCategory)) {
			Intent intent = new Intent(this, AddCategoryActivity.class);
			this.startActivity(intent);
			return;
		}
		
		if (view.getId() == R.id.ListItemCategory) {
			
			//Markiert ab hier
//			Log.d("tt", "item clicked");
//			
//			this.setItemPos(-2);
//			ls_categories.setSelection(((Integer)view.getTag()).intValue());
//			
//			mCursor_itemCategorylist.requery();
//			
//			view.requestFocus();
////			ls_categories.setSelection(((Integer)view.getTag()).intValue());
			//Markiert bis hier
			
			//test
			Intent intent = new Intent(this, SelectItemActivity.class);
			int pos = ((Integer)view.getTag()).intValue();
			mCursor_itemCategorylist.moveToPosition(pos);
			intent.putExtra(ItemCategory.ITEMCATEGORY_ID, 
					mCursor_itemCategorylist.getInt(mCursor_itemCategorylist.getColumnIndex(ItemCategory.ITEMCATEGORY_ID)));
			intent.putExtra(ItemCategory.ITEMCATEGORY_NAME, 
					mCursor_itemCategorylist.getString(mCursor_itemCategorylist.getColumnIndex(ItemCategory.ITEMCATEGORY_NAME)));
			this.startActivity(intent);
			//
			
			return;
		}
		
//		if (view.getId() == R.id.LL_Item) {
//			
//			et_item.setText("test");
//			view.requestFocus();
//			
//			return;
//		}
		
		if (view.getId() == R.id.RB_Cat) {
			
			this.setSelected(((Integer)view.getTag()).intValue());
			mCursor_itemCategorylist.requery();
		}
		
//		Button btn = (Button)view;
		if (view.getId() == R.id.Btn_ItemCategory_More) {
//			this.setItemPos(((Integer)(view.getTag())).intValue());
//			mCursor_itemCategorylist.requery();
			
			LinearLayout ll = (LinearLayout)view.getParent();
			Button btn_ItemHide = (Button)ll.findViewById(R.id.Btn_ItemCategory_Hide);
			Button btn_ItemEdit = (Button)ll.findViewById(R.id.Btn_ItemCategory_Edit);
			Button btn_ItemDelete = (Button)ll.findViewById(R.id.Btn_ItemCategory_Delete);
			Button btn_ItemMore = (Button)ll.findViewById(R.id.Btn_ItemCategory_More);
			
			btn_ItemMore.setVisibility(Button.GONE);
			
			btn_ItemHide.setVisibility(Button.VISIBLE);
			btn_ItemHide.startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_left));
			btn_ItemEdit.setVisibility(Button.VISIBLE);
			btn_ItemEdit.startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_left));
			
			int cat_id = ((Integer)view.getTag()).intValue();
			if (cat_id != 1) {
				btn_ItemDelete.setVisibility(Button.VISIBLE);
				btn_ItemDelete.startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_left));
			}
			
			return;
		}
		if (view.getId() == R.id.Btn_ItemCategory_Hide) {
			LinearLayout ll = (LinearLayout)view.getParent();
			Button btn_ItemHide = (Button)ll.findViewById(R.id.Btn_ItemCategory_Hide);
			Button btn_ItemEdit = (Button)ll.findViewById(R.id.Btn_ItemCategory_Edit);
			Button btn_ItemDelete = (Button)ll.findViewById(R.id.Btn_ItemCategory_Delete);
			Button btn_ItemMore = (Button)ll.findViewById(R.id.Btn_ItemCategory_More);
			
			btn_ItemHide.startAnimation(AnimationUtils.loadAnimation(AddItemActivity.this, R.anim.slide_right));
			btn_ItemHide.setVisibility(Button.GONE);
			btn_ItemEdit.startAnimation(AnimationUtils.loadAnimation(AddItemActivity.this, R.anim.slide_right));
			btn_ItemEdit.setVisibility(Button.GONE);
			btn_ItemDelete.startAnimation(AnimationUtils.loadAnimation(AddItemActivity.this, R.anim.slide_right));
			btn_ItemDelete.setVisibility(Button.GONE);
			btn_ItemMore.setVisibility(Button.VISIBLE);
			return;
		}
		if (view.getId() == R.id.Btn_ItemCategory_Delete) {
			
			final int itemCate_id = ((Integer)(view.getTag())).intValue();
			
			if (ShoppingListFacade.getInstance().havingItemsInCategory(itemCate_id)) {
				
				Toast.makeText(this, R.string.page_addItems_category_delete, Toast.LENGTH_SHORT).show();
				return;
			}
			
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.page_delete_question).setCancelable(false)
			.setPositiveButton(R.string.button_yes, new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   ShoppingListFacade.getInstance().deleteItemCategory(itemCate_id);
		        	   dialog.cancel();
		        	   mCursor_itemCategorylist.requery();
		           }
		       }).setNegativeButton(R.string.button_no, new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   dialog.cancel();
		           }
		       });
			AlertDialog alert = builder.create();
			alert.show();
			return;
		}
		if (view.getId() == R.id.Btn_ItemCategory_Edit) {
			
			final int itemCate_id = ((Integer)(view.getTag())).intValue();
			
			Intent intent = new Intent(this, EditActivity.class);
			intent.putExtra(ItemCategory.ITEMCATEGORY_ID, itemCate_id);
			this.startActivity(intent);
			return;
		}
	}

	public void fillItemCategoryList() {

		mCursor_itemCategorylist = ShoppingListFacade.getInstance()
				.getPersistence().fetchItemCategories();
		startManagingCursor(mCursor_itemCategorylist);

		// String[] from = new String[] { ItemCategory.ITEMCATEGORY_NAME };
		// int[] to = new int[] { android.R.id.text1 };
		//		
		// SimpleCursorAdapter icAdapter = new SimpleCursorAdapter(this,
		// android.R.layout.simple_list_item_1, mCursor_itemCategorylist, from,
		// to);
		// ls_categories.setAdapter(icAdapter);

		ls_categories.setAdapter(new ItemCategoryCursorAdapter(this,
				mCursor_itemCategorylist));
		ls_categories.setItemsCanFocus(true);
		ls_categories.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
//		ls_categories.setOnItemSelectedListener(this);
//		ls_categories.setOnItemClickListener(this);
		
//		ls_categories.setOnItemSelectedListener(this);
	}

	private class ItemCategoryCursorAdapter extends ResourceCursorAdapter {

		public ItemCategoryCursorAdapter(Context context, Cursor c) {
			super(context, R.layout.list_itemcategory, c);
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			LayoutInflater inflator = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			return inflator.inflate(R.layout.list_itemcategory, parent, false);
		}

		@Override
		public void bindView(View view, Context ctx, Cursor cursor) {
			
			TextView tv_Item = (TextView) view.findViewById(R.id.ItemCategory);
//			tv_Item.setPadding(36, 0, 0, 0);
			tv_Item.setText(cursor.getString(cursor
					.getColumnIndex(ItemCategory.ITEMCATEGORY_NAME)));

			int itemCate_id = cursor.getInt(cursor.getColumnIndex(ItemCategory.ITEMCATEGORY_ID));

			RadioButton rb_Cat = (RadioButton)view.findViewById(R.id.RB_Cat);
			rb_Cat.setTag(new Integer(cursor.getPosition()));
			rb_Cat.setChecked(cursor.getPosition() == AddItemActivity.this.getSelected());
			rb_Cat.setOnClickListener(AddItemActivity.this);
			
			Button btn_ItemHide = (Button) view
			.findViewById(R.id.Btn_ItemCategory_Hide);
			btn_ItemHide.setVisibility(Button.GONE);
			btn_ItemHide.setOnClickListener(AddItemActivity.this);
			
			Button btn_ItemEdit = (Button) view
					.findViewById(R.id.Btn_ItemCategory_Edit);
			btn_ItemEdit.setVisibility(Button.GONE);
			btn_ItemEdit.setTag(new Integer(itemCate_id));
			btn_ItemEdit.setOnClickListener(AddItemActivity.this);

			Button btn_ItemDelete = (Button) view
					.findViewById(R.id.Btn_ItemCategory_Delete);
			btn_ItemDelete.setVisibility(Button.GONE);
			btn_ItemDelete.setTag(new Integer(itemCate_id));
			btn_ItemDelete.setOnClickListener(AddItemActivity.this);

			Button btn_ItemMore = (Button) view
					.findViewById(R.id.Btn_ItemCategory_More);
			btn_ItemMore.setVisibility(Button.VISIBLE);
			btn_ItemMore.setTag(new Integer(itemCate_id));
			btn_ItemMore.setOnClickListener(AddItemActivity.this);

//			if (itemCate_id == AddItemActivity.this.getItemPos()) {
//				btn_ItemMore.setVisibility(Button.GONE);
//
//				btn_ItemEdit.setVisibility(Button.VISIBLE);
//				btn_ItemDelete.setVisibility(Button.VISIBLE);
//				btn_ItemEdit.startAnimation(AnimationUtils.loadAnimation(
//						AddItemActivity.this, R.anim.slide_left));
//				btn_ItemDelete.startAnimation(AnimationUtils.loadAnimation(
//						AddItemActivity.this, R.anim.slide_left));
//
//			} else {
//				btn_ItemEdit.setVisibility(Button.GONE);
//				btn_ItemDelete.setVisibility(Button.GONE);
//				btn_ItemMore.setVisibility(Button.VISIBLE);
//			}

			view.setTag(new Integer(cursor.getPosition()));
			view.setOnClickListener(AddItemActivity.this);
		}
	
	}
	
//	private class ItemCategoryCursorAdapter extends ResourceCursorTreeAdapter {
//
//		public ItemCategoryCursorAdapter(Context context, Cursor cur) {
//			super(context, cur, R.layout.list_itemcategory, R.layout.item);
//		}
//
//		@Override
//		protected void bindChildView(View view, Context context, Cursor cursor,
//				boolean isLastChild ) {
//			
//			TextView tv_Item = (TextView) view.findViewById(R.id.TV_Item);
//			tv_Item.setPadding(36, 0, 0, 0);
//			tv_Item.setText(cursor.getString(cursor.getColumnIndex(Item.ITEM_NAME)));
//			
//			view.setTag(new Integer(cursor.getInt(cursor.getColumnIndex(Item.ITEM_ID))));
//			view.setOnClickListener(AddItemActivity.this);
//		}
//
//		@Override
//		protected void bindGroupView(View view, Context context, Cursor cursor,
//				boolean isExpanded) {
//			
////			TextView tv_Item = (TextView) view.findViewById(android.R.id.text1);
////			tv_Item.setText(cursor.getString(cursor.getColumnIndex(ItemCategory.ITEMCATEGORY_NAME)));
//			
//			TextView tv_Item = (TextView) view.findViewById(R.id.ItemCategory);
//			tv_Item.setPadding(36, 0, 0, 0);
//			tv_Item.setText(cursor.getString(cursor
//					.getColumnIndex(ItemCategory.ITEMCATEGORY_NAME)));
//
//			int itemCate_id = cursor.getInt(cursor.getColumnIndex(ItemCategory.ITEMCATEGORY_ID));
//
//			Button btn_ItemEdit = (Button) view
//					.findViewById(R.id.Btn_ItemCategory_Edit);
//			btn_ItemEdit.setTag(new Integer(itemCate_id));
////			btn_ItemEdit.setOnClickListener(AddItemActivity.this);
//
//			Button btn_ItemDelete = (Button) view
//					.findViewById(R.id.Btn_ItemCategory_Delete);
//			btn_ItemDelete.setTag(new Integer(itemCate_id));
////			btn_ItemDelete.setOnClickListener(AddItemActivity.this);
//
//			Button btn_ItemMore = (Button) view
//					.findViewById(R.id.Btn_ItemCategory_More);
//			btn_ItemMore.setTag(new Integer(itemCate_id));
//			btn_ItemMore.setOnClickListener(AddItemActivity.this);
//
//			if (itemCate_id == AddItemActivity.this.getItemPos()) {
//				btn_ItemMore.setVisibility(Button.GONE);
//
//				btn_ItemEdit.setVisibility(Button.VISIBLE);
//				btn_ItemDelete.setVisibility(Button.VISIBLE);
//				btn_ItemEdit.startAnimation(AnimationUtils.loadAnimation(
//						AddItemActivity.this, R.anim.slide_left));
//				btn_ItemDelete.startAnimation(AnimationUtils.loadAnimation(
//						AddItemActivity.this, R.anim.slide_left));
//
//			} else {
//				btn_ItemEdit.setVisibility(Button.GONE);
//				btn_ItemDelete.setVisibility(Button.GONE);
//				btn_ItemMore.setVisibility(Button.VISIBLE);
//			}
//
////			view.setOnClickListener(AddItemActivity.this);
//		}
//
//		@Override
//		protected Cursor getChildrenCursor(Cursor groupCursor) {
//			
//			int itemCate_id = groupCursor.getInt(groupCursor.getColumnIndex(ItemCategory.ITEMCATEGORY_ID));
//			return ShoppingListFacade.getInstance().getPersistence().fetchAllItemsOfCategory(itemCate_id);
//		}
//
//		@Override
//		public View newChildView(Context context, Cursor cursor,
//				boolean isLastChild, ViewGroup parent) {
//			LayoutInflater inflator = (LayoutInflater) context
//					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//			return inflator.inflate(R.layout.item, parent, false);
//		}
//
//		@Override
//		public View newGroupView(Context context, Cursor cursor,
//				boolean isExpanded, ViewGroup parent) {
//			
//			LayoutInflater inflator = (LayoutInflater) context
//					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//			return inflator.inflate(R.layout.list_itemcategory, parent, false);
//		}
//
//		@Override
//		public void onGroupExpanded(int groupPosition) {
//			super.onGroupExpanded(groupPosition);
//		}
//
//	}
	
//	public int getItemPos() {
//		return itemPos;
//	}

//	public void setItemPos(int itemPos) {
//		this.itemPos = itemPos;
//	}

//	public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
//			long arg3) {
//		Log.d("tt", "item selected");
//	}
//
//	public void onNothingSelected(AdapterView<?> arg0) {
//		
//	}
//
//	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//		Log.d("tt", "item clicked");
//	}

	public int getSelected() {
		return selected;
	}

	public void setSelected(int selected) {
		this.selected = selected;
	}

}
